using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MomentumV2
{
    abstract class mMenuScreen : mGameScreen
    {

        #region Instance Variables

        List<mMenuEntry> m_MenuEntries = new List<mMenuEntry>();
        int m_SelectedEntry = 0;
        string m_MenuTitle;

        #endregion

        #region Properties

        protected IList<mMenuEntry> MenuEntries
        {
            get { return m_MenuEntries; }
        }

        #endregion

        #region Initialziation

        /// <summary>
        /// Constructor.
        /// </summary>
        public mMenuScreen(string menuTitle)
        {
            m_MenuTitle = menuTitle;
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// Responds to user input, changing the selected entry and accepting
        /// or cancelling the menu.
        /// </summary>
        public override void HandleInput(mInputState input)
        {
            if (input.MenuUp)
            {
                m_SelectedEntry--;
                if (m_SelectedEntry < 0)
                {
                    m_SelectedEntry = m_MenuEntries.Count - 1;
                }
            }

            if (input.MenuDown)
            {
                m_SelectedEntry++;
                if (m_SelectedEntry >= m_MenuEntries.Count)
                {
                    m_SelectedEntry = 0;
                }
            }

            if (input.MenuSelect)
            {
                OnSelectEntry(m_SelectedEntry);
            }
            else if (input.MenuCancel)
            {
                OnCancel();
            }
        }

        /// <summary>
        /// Handler for when the user has chosen a menu entry.
        /// </summary>
        protected virtual void OnSelectEntry(int entryIndex)
        {
            m_MenuEntries[m_SelectedEntry].OnSelectEntry();
        }


        /// <summary>
        /// Handler for when the user has cancelled the menu.
        /// </summary>
        protected virtual void OnCancel()
        {
            ExitScreen();
        }

        /// <summary>
        /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
        /// </summary>
        protected void OnCancel(object sender, EventArgs e)
        {
            OnCancel();
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.SpriteFont;

            Vector2 position = new Vector2(100, 300);

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            if (ScreenState == ScreenState.TransitionOn)
            {
                position.X -= transitionOffset * 256;
            }
            else
            {
                position.X += transitionOffset * 512;
            }

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < m_MenuEntries.Count; i++)
            {
                mMenuEntry menuEntry = m_MenuEntries[i];

                bool isSelected = IsActive && (i == m_SelectedEntry);

                menuEntry.Draw(this, position, isSelected, gameTime);

                position.Y += menuEntry.GetHeight(this);
            }

            // Draw the menu title.
            Vector2 titlePosition = new Vector2(90, 250);
            Vector2 titleShadowOffset = new Vector2(2, 2);
            Color titleColor = new Color(108, 80, 42, TransitionAlpha);
            Color titleShadowColor = new Color(0, 0, 0, TransitionAlpha);
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, m_MenuTitle, titlePosition + titleShadowOffset, titleShadowColor, 0,
                                   Vector2.Zero, titleScale, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, m_MenuTitle, titlePosition, titleColor, 0,
                                   Vector2.Zero, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }

        #endregion

    }
}
